<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
        .upLoadFormDadaDiv{
            position: relative;
            cursor: pointer;
        }
        .upLoadFormDadaDiv #AjaxFileData{
            position: absolute;
            top: 0;left: 0;
            opacity: 0;
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label">商户名称</label>
        <div class="layui-input-block">
            <select name="interest" lay-filter="selectSeller" id="selectSeller">
                <option value=""></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">公司名称</label>
        <div class="layui-input-block">
            <input type="text" name="sellerCompanyName" disabled autocomplete="off" class="layui-input">
            <input type="hidden" name="sellerId">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">税单月份</label>
        <div class="layui-input-block">
            <input type="text" name="startDate" id="dateStart" lay-verify="date" placeholder="税单月份" autocomplete="off" class="layui-input">
        </div>
    </div>
    <input type="hidden" name="images" class="image">
    <div class="layui-form-item">
        <label class="layui-form-label ">税单文件</label>
        <div class="layui-upload layui-input-block">
            <div class="upLoadFormDadaDiv layui-btn">
                <i class="layui-icon"></i>上传图片
                <input id="AjaxFileData" type="file" name="file">
            </div>
            <div style="margin: 10px 0">
                <img class="layui-upload-img" id="picShow" src="" height="200" onclick="show_img(this)">
                <p id="demoText"></p>
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注信息</label>
        <div class="layui-input-block">
            <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form','upload','laydate','tfApi'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            laydate = layui.laydate,
            API = layui.tfApi;

            API.postAjax("/service/qrySellerInfoByService",{},function (res) {

                var _data = res.data;

                var _h = "<option>请选择商户</option>";

                for(var i =0; i<_data.length; i++){
                    _h += "<option value='"+_data[i].sellerId+"|"+_data[i].sellerName+"|"+_data[i].sellerCompanyName+"'>"+_data[i].sellerName+"</option>"
                }
                $("#selectSeller").html(_h);

                form.render('select');


            });



        //日期
          laydate.render({
            elem: '#dateStart'
              ,type: 'month'
          });



        $("#AjaxFileData").change(function(){
            //$("#picShow").attr("src",URL.createObjectURL($(this)[0].files[0]));
            var file = $('#AjaxFileData').get(0).files[0];
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload=function(e){
                //读取成功后返回的一个参数e，整个的一个进度事件
                console.log(e);
                //选择所要显示图片的img，要赋值给img的src就是e中target下result里面
                //的base64编码格式的地址
                $('#picShow').get(0).src = e.target.result;
            }
        });
          
        //监听select  
        form.on('select(selectSeller)',function (data) {
            var _val = data.value,
                sellerCompanyName = _val.split("|")[2],
                sellerId = _val.split("|")[0];
            console.log("data=="+_val);
            $("input[name='sellerCompanyName']").val(sellerCompanyName);
            $("input[name='sellerId']").val(sellerId);

        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {

            console.log("data+==="+data.field);

            var sellerId = $("input[name='sellerId']").val(),
                taxMonth = $("input[name='startDate']").val();


            //判断文件类型
            var picFiles = $("#AjaxFileData")[0].files[0];


            console.log('AjaxFileData=111='+$("#AjaxFileData").val());

            var formData = new FormData();
            formData.append("file", picFiles);
            formData.append("sellerId", sellerId);
            formData.append("taxMonth", taxMonth);
            formData.append("taxUrl", '');

             if(picFiles==undefined || picFiles=="" || sellerId==undefined || sellerId=="" || taxMonth==undefined || taxMonth==""){
                var index=layer.alert('请输入完整的数据！', {
                    title: '提示信息'
                }, function () {
                    // 关闭弹出层
                    layer.close(index);
                });
                return;
            }

            $.ajax({
                url: API.CURL + '/service/insTax',
                type: 'post',
                data: formData,
                processData: false,
                contentType: false,
                success: function (res) {
                    console.log("ok=="+res);
                    var data = JSON.parse(res);
                    if(data.code == 0){
                        var index = layer.alert('增加税单成功！', {
                            title: '最终的提交信息'
                        }, function () {

                            // 关闭弹出层
                            layer.close(index);

                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);

                        });
                    }
                }
            });



            return false;
        });

    });
</script>
</body>
</html>